package com.cwj.课程设计;

import com.cwj.lab1.myArrayList;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 查询页表
 */

public class QueryPane {
    //四个方位的面板
    private JPanel mainPane;
    private JPanel southPane;
    private JPanel centerPane;
    private JPanel northPane;
    //图
    private Graph_Name_star graph;
    //north
    private String GraphName = "未命名";
    //图名标签
    private JLabel graphNameLab;
    //center
    //路径信息文本框
    private JTextArea pathText;
    myArrayList<Integer> path;
    double res = 0;
    //south
    //记录页表类，方便将数据传入查询页面中
    private RecordPane recordPane;
    //查询方式
    private static final String smallDis = "最短距离";
    private static final String smallTime = "最短时间";
    private static final String smallStation = "最少中转站";
    private static final String smallMoney = "最低花费";
    public static final String[] CHOOSEQUERY = {smallDis, smallTime, smallMoney, smallStation,};
    private static final int ChooseDis = 0;
    private static final int ChooseTime = 1;
    private static final int ChooseMoney = 2;
    private static final int ChooseStation = 3;
    //    查询方式选择下拉框
    private JComboBox<String> cmbQuery;
    //    开始下拉框
    private JComboBox<String> startPlane;
    //    结束下拉框
    private JComboBox<String> EndPlane;
    //    结果数值文本和标签
    private JLabel resLab;
    private JTextField resText;
    //两个按钮
    private JButton queryBut;
    private JButton saveBut;
    //最后一次查询的时间
    private Date queryDate;
//    private String text="";

    //构造函数，为了增加可读性，就将其每个部分的初始化均封装为初始化函数
    public QueryPane() {
        this.prePane();
        this.preCenter();
        this.preSouth();
        this.preNorth();
    }

    public void prePane() {
        this.mainPane = new JPanel(new BorderLayout(5, 5));
        this.northPane = new JPanel(new FlowLayout(FlowLayout.CENTER));
        this.southPane = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
        this.centerPane = new JPanel();
//        this.mainPane.add(this.pathText,BorderLayout.CENTER);
        this.mainPane.add(this.southPane, BorderLayout.SOUTH);
        this.mainPane.add(this.northPane, BorderLayout.NORTH);
        this.mainPane.add(this.centerPane);
    }

    public void preNorth() {
        this.graphNameLab = new JLabel(GraphName);
        this.northPane.add(graphNameLab);
    }

    public void preCenter() {
        this.pathText = new JTextArea(15, 45);
        this.pathText.setLineWrap(true);
        this.pathText.setEnabled(false);
        this.pathText.setFont(new Font("楷体", Font.BOLD, 16));    //修改字体样式
        //this.pathText.setForeground(Color.gray);
        JScrollPane scrollPane = new JScrollPane(this.pathText);
        this.centerPane.add(scrollPane);
    }

    public void preSouth(/*myArrayList<String> list*/) {
        this.queryBut = new JButton("查询");
        this.cmbQuery = new JComboBox<>();
        this.startPlane = new JComboBox<>();
        this.EndPlane = new JComboBox<>();
        this.resLab = new JLabel("结果:");
        this.resText = new JTextField(8);
        this.saveBut = new JButton("保存");

        this.resText.setEditable(false);

        this.cmbQuery.addItem(QueryPane.smallDis);
        this.cmbQuery.addItem(QueryPane.smallTime);
        this.cmbQuery.addItem(QueryPane.smallMoney);
        this.cmbQuery.addItem(QueryPane.smallStation);
        //处理
        //this.preSouthCmb(list);
        this.queryBut.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                query();
            }
        });
        //保存按钮
        this.saveBut.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //如果路径文本为空串就直接结束
                if (pathText.getText().equals("")) {
                    return;
                }
                //其为记录的String
                String[] strList = {cmbQuery.getSelectedItem().toString(), graph.getGraphName(),pathText.getText()/* graph.ArrayToPath(path)*/, resText.getText() + "", getQueryDate()};
                if (recordPane == null) {
                    return;
                }
                recordPane.addRow(strList);
            }
        });


        this.southPane.add(this.cmbQuery);
        this.southPane.add(this.startPlane);
        this.southPane.add(this.EndPlane);
        this.southPane.add(this.queryBut);
        this.southPane.add(this.resLab);
        this.southPane.add(this.resText);
        this.southPane.add(this.saveBut);
    }

    //查询函数
    public void query() {
        int ind = this.cmbQuery.getSelectedIndex();
        int start = this.startPlane.getSelectedIndex();
        int end = this.EndPlane.getSelectedIndex();
        path = null;
        res = 0;
        if (start == -1 || end == -1) {
            return;
        }
        switch (ind) {
            case QueryPane.ChooseDis:
                path = this.graph.getDisPath(start, end);
                res = this.graph.getSmallDis(start, end);
                break;
            case QueryPane.ChooseTime:
                path = this.graph.getTimePath(start, end);
                res = this.graph.getSmallTime(start, end);
                break;
            case QueryPane.ChooseMoney:
                path = this.graph.getMoneyPath(start, end);
                res = this.graph.getSmallMoney(start, end);
                break;
            case QueryPane.ChooseStation:
                path = this.graph.getTransferPath(start, end);
                res = this.graph.getSmallTransferVal(start,end);
                break;
            default:
                return;
        }
        if (path == null) {
            return;
        }
        String strPath = null;
        String resStr = null;
        //不连通识别
        if (res == Graph_Name_star.INF) {
            strPath = "不连通";
            resStr = "";
        } else {
            strPath = this.graph.ArrayToPath(path);
            resStr = res + "";
        }
        this.pathText.setText(strPath);
        this.resText.setText(resStr);
        this.queryDate = new Date();
    }

    //得到最后一次查询时间
    public String getQueryDate() {
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss");
        return ft.format(this.queryDate);
    }

    //初始化下拉框
    public void preSouthCmb(myArrayList<String> nodeList) {
        for (int i = 0; i < nodeList.size(); i++) {
            this.startPlane.addItem(nodeList.get(i));
            this.EndPlane.addItem(nodeList.get(i));
        }
    }

    //该函数为修改图后重新载入查询表的函数，其紧接着的函数就是其对应得封装函数
    public void preGraph() {
        this.GraphName = this.graph.getGraphName();
        this.rePreSouth();
        this.rePreNorth();
    }

    private void rePreSouth() {
        String name = null;
        this.startPlane.removeAllItems();
        this.EndPlane.removeAllItems();
        int Len = this.graph.getV_num();
        for (int i = 0; i < Len; i++) {
            this.startPlane.addItem(name = this.graph.getNodeName(i));
            this.EndPlane.addItem(name);
        }
    }

    private void rePreNorth() {
        this.graphNameLab.setText(this.graph.getGraphName());
    }

    public void setGraphName(String graphName) {
        this.graphNameLab.setText(graphName);
    }

//    public void setText(String text) {
//        this.text = text;
//    }

    public JPanel getMainPane() {
        return mainPane;
    }

    public void setGraph(Graph_Name_star graph) {
        this.graph = graph;
    }

    public void setRecordPane(RecordPane recordPane) {
        this.recordPane = recordPane;
    }

    //清空按钮
    public void clearPane() {
        this.graphNameLab.setText("未命名");
        this.pathText.setText("");
        //this.cmbQuery.removeAllItems();
        this.startPlane.removeAllItems();
        this.EndPlane.removeAllItems();
    }

    public static void main(String[] args) {
        JFrame my = new JFrame();
        my.setContentPane(new QueryPane().mainPane);
        my.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        my.pack();
        my.setVisible(true);
    }
}
